package com.bignerdranch.android.furnitureapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bignerdranch.android.furnitureapplication.net.LoginResponse;
import com.bignerdranch.android.furnitureapplication.net.RetrofitHelper;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText userName;
    private EditText userPass;
    private Button submitButton;
    private TextView toRegister;
    private LoginResponse mLoginResponse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initViews();
    }

    private void initViews() {
        userName = findViewById(R.id.login_phone);
        userPass = findViewById(R.id.login_pwd);
        submitButton = findViewById(R.id.submit_button);
        toRegister = findViewById(R.id.registered);

        submitButton.setOnClickListener(this);
        toRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                String getUserName = userName.getText().toString();
                String getUserPass = userPass.getText().toString();
                if (getUserName.length() != 0 && getUserPass.length() != 0) {
                    login(getUserName, getUserPass);
                } else {
                    Toast.makeText(this, "请输入完整的用户名与密码", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.registered:
                startActivity(new Intent(this, RegisterActivity.class));
                break;
        }
    }

    private void login(final String userAccount, String userPassword) {
        RetrofitHelper.getAccountService()
                .test_loginCheck(userAccount, userPassword)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            startActivity(new Intent(LoginActivity.this, MainActivity.class));
                            SharedPreferences preferences = getSharedPreferences("user_info",MODE_PRIVATE);
                            //得到SharePreferences.Editor对象,并保存数据到该对象中
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putString("user_account", userAccount);
                            //保存key-value对到文件中
                            editor.apply();
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, "帐号或密码错误！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(LoginActivity.this, "网络异常!", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }
}
